package com.titaniue.java;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 *@program: JavaSenior
 *@description:
 * 1.RandomAccessFile直接继承与java.lang.Object类，实现了DataInput和DataOutPut接口
 * 2.RandomAccessFile既可以作为一个输入流，又可以作为一个输出流
 * 3.如果RandomAccessFile 作为输出流时，写出到的文件如果不存在，则在执行过程中自动创建
 * 乳沟写出到文件存在，则会对源文件内容进行覆盖。（默认情况下，从头覆盖）
 *
 *@author: renfei.huang
 *@create: 2021-10-18 11:06
 */
public class RandomAccessFileTest {




    //  复制图片操作
    @Test
    public  void test1(){

        RandomAccessFile randomAccessFile1 = null;
        RandomAccessFile randomAccessFile2 = null;

        try {
             randomAccessFile1 = new RandomAccessFile(new File("彼岸图网.jpg"), "r");
             randomAccessFile2 = new RandomAccessFile(new File("彼岸图网2.jpg"), "rw");

            byte [] buffer = new byte[1024];
            int let;
            while ((let = randomAccessFile1.read(buffer))!= -1){
                randomAccessFile2.write(buffer,0,let);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomAccessFile1 != null) {
                try {
                    randomAccessFile1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (randomAccessFile2 == null) {
                try {
                    randomAccessFile2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }


    @Test
    public void test2(){

        RandomAccessFile accessFile = null;
        try {
            accessFile = new RandomAccessFile(new File("hello.txt"), "rw");
            accessFile.seek(3);  //指针调到脚标为3的位置
            accessFile.write("xyz".getBytes()); //覆盖数据（有数据）
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (accessFile != null) {
                try {
                    accessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }


    /*
    *
    *
    * 使用RandomAccessFile 是数据插入的效果
    *
    *
    * */
    @Test
    public void test3(){

        RandomAccessFile accessFile = null;
        try {
            accessFile = new RandomAccessFile(new File("hello.txt"), "rw");
            accessFile.seek(3);  //指针调到脚标为3的位置
            //保存指针3后吗的所有数据到StringBuilder中
            StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
            byte [] buffe = new byte[20];
            int len;
            while ((len = accessFile.read(buffe))!= -1){
                builder.append(new String(buffe,0,len));
            }

            //调回指针，写入"xyz"
            accessFile.seek(3);  //指针调到脚标为3的位置
            accessFile.write("xyz".getBytes());

            //将StringBuilder中内容写入到文件中
            accessFile.write(builder.toString().getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (accessFile != null) {
                try {
                    accessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}
